<!--
 * @Description: 我发起的数据（当前模板）
-->
<template>
  <div class="my-data">
    <div class="relation-data-title">
      <div>
        {{ data.subject }}
        <a-tooltip placement="leftTop" v-if="!isSummary && isCanCopy">
          <template slot="title"> <div v-html="helpText"></div> </template
          ><icon-font type="cap-icon-weibiaoti6-copy" class="icon-font" />
        </a-tooltip>
      </div>
      <span
        v-if="data.showMore"
        class="title-more"
        :href="moreUrl"
        @click="open"
        target="_blank"
        style="font-weight: 500;"
        >{{ moreLabel }}</span
      >
    </div>
    <ul>
      <li v-for="item in list" :key="item.affairId">
        <a-tooltip :title="item.subject">
          <a
            :href="`${ctxPath}/collaboration/collaboration.do?method=summary&openFrom=glwd&affairId=${item.affairId}&baseObjectId=${item.summaryId}&baseApp=1`"
            target="_blank"
            v-html="createTitle(item)">
          </a>
        </a-tooltip>
          <a-tooltip :title="$i18n('datarelation.tab.copy.node.js')"
                     v-if="!isSummary && isCanCopy && item.templeteId && item.templeteId === currentTemplateId">
            <icon-font class="toback_16" type="cap-icon-fuzhi1" @click="copyFormData(item)" />
        </a-tooltip>
      </li>
    </ul>
  </div>
</template>
<script>
  /*global openCtpWindow*/
  import { useTemplateStore } from '@/store/modules/template';
  import { mapWritableState } from 'pinia';
  import emitter from '@/plugins/global-callback';
  import globalData from '@/plugins/data';
  import mixins from '../mixins';

  export default {
    name: 'MyData',
    mixins: [mixins],
    data() {
      return {
        moreLabel: this.$i18n('common.more.label'),
        currentTemplateId: this.baseInfo.businessData?.template?.id
      };
    },
    computed: {
      ...mapWritableState(useTemplateStore, [
        'zwIframe',
        'isIframeReady',
        'zwIframeUrl',
        'zwIframeUrlCopy'
      ]),
      isCap4() {
        return this.baseInfo.zwIframe.isCap4;
      },
      //问号移上去的帮助性文案
      helpText() {
        return `${$.i18n('datarelation.note.copyform.title.js')}
                <br>
                ${$.i18n('datarelation.note.copyform.note1.js')}
                <br>
                ${$.i18n('datarelation.note.copyform.note2.js')}
                `;
      },
      moreUrl() {
        // if (!this.list.length) return;
        const dataTypeName = this.data.dataTypeName;
        //新建页面openFrom始终为newColl
        const openFrom = globalData.isSummary? globalData.openFrom : 'newColl';
        const { canCopy, bodyType } = this.baseInfo.businessData?.template||{canCopy:false,bodyType:10};
        const { affair, summary, template = {} } = this.baseInfo.businessData;
        const formAppId = summary?.formAppid || affair?.formAppId || '';
        const method = this.isSummary && affair.state !== 1 && affair.state !== 2 ? 'templateDealMore' : 'templateSendMore';
        const pid = this.data.id;
        const summaryId = summary.id;
        const senderId = affair.senderId || '';
        const memberId = affair.memberId || '';
        const nodePolicy = affair.nodePolicy || '';
        const templateId = affair.templateId || summary.templateId || template.id;
        if (dataTypeName === 'selfColl') {
          return `${globalData.ctxPath}/dataRelation.do?method=selfCollMore&pid=${pid}&summaryId=${summaryId}&senderId=${senderId}&memberId=${memberId}&affairId=${affair.id}&subject=${encodeURIComponent(
              this.data.subject
          )}&sendToOther=${this.data.sendToOther}`
        }
        return `${
          globalData.ctxPath
        }/dataRelation.do?method=${method}&openFrom=${openFrom}&summaryId=${summaryId}&senderId=${senderId}&memberId=${memberId}&nodePolicy=${nodePolicy}&pid=${pid}&affairId=${
          affair.id || '-1'
        }&bodyType=${bodyType}&canCopy=${canCopy}&formAppid=${formAppId}&templateId=${templateId}&subject=${encodeURIComponent(
          this.data.subject
        )}&cap4Flag=true`;
      }
    },
    created() {
      this.ctxPath = globalData.ctxPath;

      //点击更多，选了复制至当前模板触发的函数
      emitter.subscribe('copyForm', this.copyFormData);
    },
    beforeDestroy() {
      emitter.unsubscribe('copyForm', this.copyFormData);
    },
    methods: {
      /**
       * @description: 生成标题。包含图标和内容
       * @param {Object}item 当前数据对象
       * @return {string}图标class
       */
      createTitle(item) {
        let title = '';
        //重要程度
        if (item.importantLevel !== '' && item.importantLevel !== 1) {
          title += ` <i class="pre-icon ico16 important${item.importantLevel}_16"></i>`;
        }

        title += `<span>${item.subject}</span>`;

        //附件
        if (item.hasAttsFlag === true) {
          title += ` <i class="ico16 affix_16"></i>`;
        }

        //协同类型
        if (item.bodyType !== '10' && item.bodyType !== '30' && item.bodyType !== '90') {
          title += ` <i class="ico16  office${item.bodyType}_16"></i>`;
        }
        return title;
      },
      /**
       * @description: 复制目标表单数据到当前模板
       * @param {Object}item 当前待复制数据对象
       */
      copyFormData({ summaryId, affair: { formRecordid } }) {
        console.log('this.isCap4', this.isCap4);

        if (this.isCap4) {
          this.isIframeReady = false;
          try {
            this.zwIframe.contentWindow.contentAPI.setFormContentCopy(
              {
                moduleType: '1',
                formRecordid,
                summaryId
              },
              () => {
                this.isIframeReady = true;
              }
            );
          } catch {
            this.isIframeReady = true;
          }
        } else {
          // var tempZwOfficeFrame = $('#zwOfficeIframe');
          // var hasLoad = 'false';
          // if (tempZwOfficeFrame) {
          //     hasLoad = tempZwOfficeFrame.attr('hasLoad'); // 获取frame中的hasLoad值
          // }
          // if (hasLoad == 'true') {
          //     // 已打开过office正文,先切换到表单视图
          //     var win = $(window.frames['zwIframe'].document);
          //     var id = win.find('#viewsTab li:first-child').attr('index');
          //     var _zwIframe = window.document.zwIframe;
          //     _zwIframe._viewContentSwitch(this, id, _zwIframe._changeViewHandler);
          // }
          // var zwIframe = _getZWIframeElement4DR();
          // if (!pageX.url) {
          //     pageX.url = zwIframe.src;
          // }
          // if (typeof _exeContentLoadedCallbackFlag != 'undefined') {
          //     _exeContentLoadedCallbackFlag = false;
          // }
          // var url =
          //     pageX.url +
          //     '&openFrom=dataRelation&formRecordid=' +
          //     formRecordid +
          //     '&summaryId=' +
          //     $('#id', $('#colMainData')).val();
          // zwIframe.src = url;
          this.zwIframeUrl = `${this.zwIframeUrlCopy}&openFrom=dataRelation&formRecordid=${formRecordid}&summaryId=${summaryId}`;
        }
      },
      open() {
        openCtpWindow({
          url: this.moreUrl,
          id: 'templateSend'
        });
      }
    }
  };
</script>
<style lang="scss" scoped>
  .my-data {
    .toback_16 {
      font-size: 16px;
      color: $icon-color;
      position: relative;
      float: right;
      margin-left: 5px;
    }

    > ul {
      font-size: 14px;
      margin-bottom: 10px;
      > li {
        display: flex;
        align-items: center;
        padding: 5px;
        margin-top: 5px;
        transition: background-color $tn linear;

        > a {
          flex: 1;
          display: flex;
          align-items: center;
          width: 80%;
          > ::v-deep span {
            @include ellipsis;

            max-width: 85%;
          }

          &::before {
            margin-right: 12px;
            content: '';
            width: 6px;
            height: 6px;
            background-color: $icon-color;
            background-color: var(--theme-brand6, $icon-color);
            border-radius: 50%; /* 将方块变成圆形 */
            flex-shrink: 0;
          }
          ::v-deep .ico16 {
            flex-shrink: 0;
          }
        }

        > i {
          visibility: hidden;
        }

        &:hover {
          background-color: #e6f6ff;

          > i {
            visibility: visible;
          }
        }
      }
    }
    ::v-deep .pre-icon {
      margin-right: 4px;
    }
  }
</style>
